<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
$_SESSION['navigation'] = 'brefs_admin';
if(!check_admin()) redirect('perso.php?action=admin');
else{
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	if(isset($_REQUEST['filtre_contenu']) && isset($_SESSION['filtre_contenu'])) unset($_SESSION['filtre_contenu']);
	$filtre_contenu = (!empty($_REQUEST['filtre_contenu'])) ? $_REQUEST['filtre_contenu'] : "";
	$_SESSION['filtre_contenu'] = ($filtre_contenu) ? $filtre_contenu : $_SESSION['filtre_contenu'];
	$_SESSION['bref_id'] = $id;
	$user_id = check_cookie();
	if($action == 'del'){
		if($id){
			$bref = new bref($id);
			echo ($bref->delete()) ? "Br&egrave;ve effac&eacute;e" : "D&eacute;sol&eacute;, une erreur est survenue";
		}
	}
	elseif($action == 'new'){
		echo ($id) ? "<h1>Modifier une br&egrave;ve</h1>" : "<h1>Nouvelle br&egrave;ve</h1>\n";
		$user_id = check_cookie();
		if($id) $bref = new bref($id);
		echo "<div style = 'position: relative; float: left; width: 350px;'>\n";
		echo "<form action = 'brefs_admin.php' method = 'post' id = 'myform' name = 'myform'>\n";
			$content = "<button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.contenu);preview(document.myform.contenu.value,\"preview\");'>_italique_</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.contenu);preview(document.myform.contenu.value,\"preview\");'>*gras*</button>\n";
			$content .= "<button type = 'button' name = 'couleur' onclick = 'inserttag(\"!!\",document.myform.contenu);preview(document.myform.contenu.value,\"preview\");'>!!couleur!!</button>\n";
			$content .= "<button type = 'button' name = 'citation' onclick = '&#39; \",document.myform.contenu);preview(document.myform.contenu.value,\"preview\");'>lien</button>\n";
			echo leftCol("",30);
			echo rightCol($content,68);
			echo leftCol("Corps: ",30);
			$value_contenu = ($id) ? $bref->contenu : "";
			echo rightCol("<textarea name = 'contenu' cols = '20' rows = '10' onkeyup = 'preview(this.value,\"preview\")'>$value_contenu</textarea>",68);
			echo leftCol("Date publication",30);
			$value_creation = ($id) ? $bref->date_creation : date('Y-m-d');
			$creation = explode("-",$value_creation);
			echo rightCol("<input class = 'center' type = 'text' size = '2' name = 'creation_jour' value = '$creation[2]'/>.<input class = 'center' type = 'text' size = '2' name = 'creation_mois' value = '$creation[1]'/>.<input class = 'center' type = 'text' size = '4' name = 'creation_annee' value = '$creation[0]'/>",68);
			echo leftCol("Date expiration",30);
			$value_expiration = ($id) ? $bref->date_expiration : date('Y-m-d');
			$expiration = explode("-",$value_expiration);
			echo rightCol("<input class = 'center' type = 'text' size = '2' name = 'expiration_jour' value = '$expiration[2]'/>.<input class = 'center' type = 'text' size = '2' name = 'expiration_mois' value = '$expiration[1]'/>.<input class = 'center' type = 'text' size = '4' name = 'expiration_annee' value = '$expiration[0]'/>",68);

			echo leftCol("Publi&eacute;e",30);
			$checked = ($id) ? (($bref->valide) ? "checked = 'checked'" : "") : "";
			echo rightCol("<input type = 'checkbox' name = 'valide' value = '1' $checked />",68);
			echo leftCol("",30);
			$print = ($id) ? "<input type = 'hidden' name = 'id' value = '$id' />\n" : "";
			$right = "<p class  ='center'>$print\n";
			$right .= "<button type = 'button' onclick = 'list_brefs()'>annuler</button>&nbsp;\n";
			$right .= "<input type = 'hidden' name = 'action' value = 'add' />\n";
			$right .= "<button type = 'submit' name = 'submit' value = 'add'>enregistrer</button></p>\n";
			echo rightCol($right,68);
			echo "<br class = 'clear' />\n";
		echo "</form></div>\n";
		echo "<div style = 'position: relative; float: left; width: 170px; margin: 40px;'>\n";
		box("en bref","<ul><li id  = 'preview'></ul>");
		echo "</div>\n";
		echo "<br class = 'clear'>\n";
	}
	elseif($action == 'list'){
		echo "<h1>Les br&egrave;ves</h1>\n";
		$table = new table();
		$fields = array("date_creation" => "creation","date_expiration" => "expiration","contenu" => "texte","valide" => "valide","actions" => "actions");
		foreach($fields as $field => $print){
			$$field = new table_field($field);
			$$field->print = $print;
			if($field != 'contenu') $$field->styleclass = 'center';
			$table->fields[] = $$field;
		}
		$contenu->styleclass = "";
		$date_expiration->filter = 0;
		$valide->filter = 0;
		$date_creation->filter = 0;
		$table->sense = "DESC";
		$table->order =  "date_expiration";
	#	$table->styleclass = 'bold_hover';
		$table->filter = 1;
		$table->nb_rows = 15;
		$q = "select * from brefs";
		$r = ExecRequete($q);
		echo "<p class = 'center'><button type = 'button' onclick = 'new_bref()'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvelle br&egrave;ve</button></p>\n";
		while($s = LigneSuivante($r)){
			$bref = new bref($s->id);
			$row = new table_row();
			foreach($fields as $field => $print){
				if($field == 'contenu'){
					// $print = (strlen($bref->$field) > 30) ? substr($bref->$field,0,30)."..." : $bref->$field;
					$print = $bref->$field;
					if($filtre_contenu) $print = str_ireplace("$filtre_contenu","<span class = 'highlight'>$filtre_contenu</span>",$print);
					if($bref->date_creation > '2008-02-01') $print = prepare_info($print);
				}
				elseif($field == 'date_creation') $print = $bref->date_creation;
				elseif($field == 'date_expiration') $print = $bref->date_expiration;
				elseif($field == 'valide') $print = ($bref->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
				elseif($field == 'actions'){
					$print =  "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' style = 'cursor:pointer' onclick = 'edit_bref($bref->id)'\"/>";
					$print .=  "&nbsp;<img src = 'images/icon_delete.png' alt = 'del' height = '14' width = '14' style = 'cursor:pointer' onclick = 'if(confirm(\"Confirmer la suppression de l bref\"))del_bref($bref->id)' />";
				}
				$row->cols[$field] = $print;
				$row->hidden = ($bref->contenu) ? 0 : 1;
			}
			$table->rows[] = $row;
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();

	}
	else{
		entete('VBCLL - Bienvenue',"","",1);
		if($action == 'add'){
			$fields = array("contenu","valide","creation_jour","creation_mois","creation_annee","expiration_jour","expiration_mois","expiration_annee");
			foreach($fields as $field){
				$$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
				error_log($field." => ".$$field);
			}
			$new_bref = ($id) ? new bref($id) : new bref('new');
			$new_bref->contenu = $contenu;
			$new_bref->date_creation = $creation_annee."-".$creation_mois."-".$creation_jour;
			$new_bref->date_expiration = $expiration_annee."-".$expiration_mois."-".$expiration_jour;
			$new_bref->valide = $valide;
			if($id){
				if($new_bref->update()){
					$texte = "mise &agrave; jour r&eacute;ussie";
				}
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
			}
			else{
				$result = $new_bref->insert();
				if($result){
					$texte = $result;
					unset($_SESSION['rand']);
				}
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";
			}
			echo "<script type = 'text/javascript'>message(\"$texte\")</script>";
		}
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_brefs(1)</script>\n";
		pied();
	}
}

?>